package com.ctu.study
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.engine.*;
	import flash.ui.Keyboard;
	
	import mx.containers.Box;
	import mx.containers.Canvas;
	import mx.controls.Label;
	import mx.core.Application;
	import mx.effects.Fade;
	import mx.events.EffectEvent;
				
	public class SimpleTextEdit
	{
		private var baseCanvas:Canvas;
		private var label:Label;
		private var container:Sprite;
	  	private var lineArr:Array;
	  	private var tb:TextBlock;
	  	private var str:String;
	  	private var te:TextElement;
	  	private var typeCursor:TypeCursor;
	  	private var cursorBox:Box;
	  	private var fadeOut:Fade;
	  	private var fadeIn:Fade;
		
		public function SimpleTextEdit(app:Application)
		{
			baseCanvas = new Canvas();
			baseCanvas.width = 600;
			baseCanvas.height = 400;
			baseCanvas.x = 50;
			baseCanvas.y = 50;
			baseCanvas.setStyle("backgroundColor", 0xFFFFFF);
			
			label = new Label();
			label.text = "Text Edit Sample";
			label.setStyle("color", 0xE3E3E3);
			label.x = 50;
			label.y = 10;
			app.addChild(label);
			app.addChild(baseCanvas);
			container = new Sprite();
			var fd:FontDescription = new FontDescription();
			fd.fontName = "맑은 고딕"
            var ef:ElementFormat = new ElementFormat(fd);
            ef.fontSize = 18;
			str = "";
			tb = new TextBlock();
			te = new TextElement(str, ef);
			tb.content = te;
			lineArr = new Array();
			createLines();
		
			trace("lineArr length:"+lineArr.length);		
			
			baseCanvas.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 0);
			baseCanvas.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0);
			baseCanvas.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0);
			baseCanvas.addEventListener(Event.ACTIVATE, activateHandler, false, 0);
			baseCanvas.addEventListener(FocusEvent.FOCUS_IN, onFocusInHandler, false, 0);
			baseCanvas.rawChildren.addChild(container);
			baseCanvas.setFocus();
		}
		
		private function activateHandler(event:Event):void{
			trace("container has been activated");
			baseCanvas.setFocus();
		}
		
		private function onFocusInHandler(event:FocusEvent):void{
			trace("onFocuseHandler");
			
		}
		
		public function endCursorFadeOut(event:EffectEvent):void {
			fadeIn.play();
		}
		
		public function endCursorFadeIn(event:EffectEvent):void {
			fadeOut.play();
		}
		
		private function onMouseDown(event:MouseEvent):void{
			trace("onMouseDown");
			baseCanvas.setFocus();
			if(typeCursor == null){
				typeCursor = new TypeCursor(0, 0);
				fadeOut = new Fade();
				fadeOut.alphaFrom = 1.0;
				fadeOut.duration = 400;
				fadeOut.alphaTo = 0.0;
				
				fadeIn = new Fade();
				fadeIn.alphaFrom = 0.0;
				fadeIn.duration = 400;
				fadeIn.alphaTo = 1.0;
				
				fadeIn.target = typeCursor;
				fadeOut.target = typeCursor;
				fadeOut.addEventListener(EffectEvent.EFFECT_END, endCursorFadeOut);
				fadeIn.addEventListener(EffectEvent.EFFECT_END, endCursorFadeIn);
				
				typeCursor.x = 0;
				typeCursor.y = 30;
				container.addChild(typeCursor);
				fadeOut.play();
				trace("cursor created");
			}
		}
		
		private function textInputHandler(event:TextEvent):void{
			str += event.text;
			te.replaceText(te.text.length, te.text.length, event.text);
			createLines();
			
			var lastLineBound:Rectangle = tb.lastLine.getBounds(container);
			typeCursor.x = lastLineBound.x + lastLineBound.width;
			typeCursor.y = lastLineBound.y;
		}
		
		private function createLines():void{
			if(lineArr.length >0){	
				container.removeChild(lineArr[lineArr.length-1]); // remove last line
				lineArr = lineArr.slice(0, lineArr.length-1);
			}
			
			var lastLine:TextLine = tb.lastLine;
			var previousLine:TextLine = null;
			var line:TextLine = null;
			
			if(lastLine == null){
				line = tb.createTextLine(null, 600);
				if(line == null){
					return;
				}
				line.x = 0;
				line.y = 30;
				lineArr.push(line);
				container.addChild(line);
			} else {
				previousLine = lastLine.previousLine;
				if(previousLine == null){
					line = tb.createTextLine(null, 600);
						if(line == null){
						return;
					}
					line.x = 0;
					line.y = 30;
					lineArr.push(line);
					container.addChild(line);
				} else {
					line = tb.createTextLine(previousLine, 600);
					line.x = 0;
					line.y = previousLine.getBounds(container).y + previousLine.height + 6;
					lineArr.push(line);
					container.addChild(line);
				}
			}
		}
		
		private function onKeyDown(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.BACKSPACE){
				if(te.text.length > 0){
					str = str.substr(0, str.length-1);
					te.replaceText(te.text.length-1, te.text.length, null);
					createLines();
				}
			}
		}

	}
}